package com.ruoyi.project.mahjong.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;

import com.ruoyi.project.mahjong.domain.*;
import com.ruoyi.project.mahjong.domain.enu.MathDeskState;
import com.ruoyi.project.mahjong.domain.enu.PersonState;
import com.ruoyi.project.mahjong.domain.enu.TablenumberState;
import com.ruoyi.project.mahjong.domain.vo.DeskIngVo;
import com.ruoyi.project.mahjong.service.*;

import com.ruoyi.project.system.domain.SysUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.mahjong.mapper.DeskIngMapper;
import org.springframework.transaction.annotation.Transactional;

/**
 * 开桌情况Service业务层处理
 *
 * @author ruoyi
 * @date 2023-07-26
 */
@Service
public class DeskIngServiceImpl implements IDeskIngService {
    @Autowired
    private DeskIngMapper deskIngMapper;
    @Autowired
    private IDeskMemberService deskMemberService;
    @Autowired
    private ITablePoolService tablePoolService;
    @Autowired
    private  IDeskIngService deskIngService;
    @Autowired
    private ITMemberService tMemberService;

    @Autowired
    private ITTablenumberService tTablenumberService;


    /**
     * 查询开桌情况
     *
     * @param id 开桌情况ID
     * @return 开桌情况
     */
    @Override
    public DeskIng selectDeskIngById(Long id) {
        return deskIngMapper.selectDeskIngById(id);
    }

    /**
     * 查询开桌情况列表
     *
     * @param deskIng 开桌情况
     * @return 开桌情况
     */
    @Override
    public List<DeskIng> selectDeskIngList(DeskIng deskIng) {
        return deskIngMapper.selectDeskIngList(deskIng);
    }

    /**
     * 新增开桌情况
     *
     * @param deskIng 开桌情况
     * @return 结果
     */
    @Override
    public int insertDeskIng(DeskIng deskIng) {
        deskIng.setCreateTime(DateUtils.getNowDate());
        return deskIngMapper.insertDeskIng(deskIng);
    }

    /**
     * 修改开桌情况
     *
     * @param deskIng 开桌情况
     * @return 结果
     */
    @Override
    public int updateDeskIng(DeskIng deskIng) {

        deskIng.setUpdateTime(DateUtils.getNowDate());
        SysUser user = SecurityUtils.getLoginUser().getUser();
        String nickName = user.getNickName();
        if (nickName!=null){
            deskIng.setUpdateBy(nickName);
        }
        return deskIngMapper.updateDeskIng(deskIng);
    }

    /**
     * 批量删除开桌情况
     *
     * @param ids 需要删除的开桌情况ID
     * @return 结果
     */
    @Override
    public int deleteDeskIngByIds(Long[] ids) {
        return deskIngMapper.deleteDeskIngByIds(ids);
    }

    /**
     * 删除开桌情况信息
     *
     * @param id 开桌情况ID
     * @return 结果
     */
    @Override
    public int deleteDeskIngById(Long id) {
        return deskIngMapper.deleteDeskIngById(id);
    }

    @Override
    public List<DeskIngVo> chengjuList(DeskIng deskIng) {
        List<DeskIngVo> voList = new ArrayList<>();

        List<DeskIng> deskIngList = deskIngMapper.selectDeskIngList(deskIng);
        if (ObjectUtil.isNotEmpty(deskIngList)) {
            for (DeskIng ing : deskIngList) {
                DeskIngVo vo = new DeskIngVo();
                BeanUtils.copyProperties(ing, vo);
                if (ObjectUtil.isNotNull(vo.getStartTime())) {
                    vo.setDuration(DateUtils.getDatePoor(DateUtils.getNowDate(), vo.getStartTime()));
                }
                DeskMember member = new DeskMember();
                member.setDeskId(ing.getId());
                List<DeskMember> memberList = deskMemberService.selectDeskMemberList(member);
                for (int i = 0; i < memberList.size(); i++) {
                    DeskMember deskMember = memberList.get(i);
                    TMember tMember = tMemberService.selectTMemberById(deskMember.getMemberId());
                    if (i == 0) {
                        vo.setOneId(deskMember.getMemberId());

                        vo.setOneName(tMember.getName());
                    }
                    if (i == 1) {
                        vo.setTwoId(deskMember.getMemberId());
                        // TMember two= tMemberService.selectTMemberById(deskMember.getMemberId());
                        vo.setTwoName(tMember.getName());
                    }
                    if (i == 2) {
                        vo.setThreeId(deskMember.getMemberId());
                        //TMember three= tMemberService.selectTMemberById(deskMember.getMemberId());
                        vo.setThreeName(tMember.getName());
                    }
                    if (i == 3) {
                        vo.setFourId(deskMember.getMemberId());
                        // foure= tMemberService.selectTMemberById(deskMember.getMemberId());

                        vo.setFourName(tMember.getName());
                    }
                }
                voList.add(vo);
            }
        }
        /*if (ObjectUtil.isNotEmpty(voList)){
        if (voList.get(0).getMode().equals("america")){
            voList.get(0).setMode("美国麻将");
        }
        if (voList.get(0).getMode().equals("common")){
            voList.get(0).setMode("普通麻将");
        }
        if (voList.get(0).getMode().equals("competition")){
            voList.get(0).setMode("比赛麻将");
        }
        if (voList.get(0).getMode().equals("custom_integral")){
            voList.get(0).setMode("自定义模式");
        }}*/
        return voList;
    }

    @Override
    public void goStart(Long id) {
//        先修改开桌表信息
        DeskIng deskIng = new DeskIng();
        deskIng.setId(id);
        deskIng.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        deskIng.setStatus(MathDeskState.START.getValue());
        deskIng.setStartTime(DateUtils.getNowDate());
        updateDeskIng(deskIng);
        deskIng=selectDeskIngById(id);
        tablePoolService.goStart(deskIng.getDeskId());

//        修改桌号状态
        TTablenumber tTablenumber = new TTablenumber();
        tTablenumber.setStatus(TablenumberState.START.getValue());
        tTablenumber.setId(deskIng.getDeskId());
        tTablenumberService.updateTTablenumber(tTablenumber);
    }
    @Override
    public void endRollback(Long id) {

//        修改开桌信息
        DeskIng deskIng = new DeskIng();
        deskIng.setId(id);
        deskIng.setStatus(MathDeskState.DP.getValue());
        updateDeskIng(deskIng);
//       查询这个桌号id
        DeskIng deskIng1 = selectDeskIngById(id);
        TTablenumber tTablenumber = new TTablenumber();
//        修改这个id的桌号状态
        tTablenumber.setId(deskIng1.getDeskId());
        tTablenumber.setStatus(TablenumberState.NO_LOCK.getValue());
//        修改配卓池里这四人的排桌状态为要考虑
        TablePool tablePool = new TablePool();
        tablePool.setDeskNumber(deskIng1.getDeskId());
       // List<TablePool> tablePools = tablePoolService.selectTablePoolListmy(tablePool);
        List<TablePool> tablePools2 = tablePoolService.selectTablePoolList(tablePool);
       /* for (TablePool pool : tablePools) {
            pool.setIsState("0");
            pool.setIsStart("0");
            pool.setState(PersonState.KL.getValue());
            try {

                tablePoolService.updateTablePool(pool);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }*/
        for (TablePool pool : tablePools2) {

            pool.setState(PersonState.KL.getValue());
            pool.setIsStart("0");
            try {
                tablePoolService.updateTablePool(pool);
            } catch (Exception e) {
                System.out.println("修改排桌状态异常");
                throw new RuntimeException(e);
            }
        }
        tTablenumberService.updateTTablenumber(tTablenumber);
    }
    @Override
    @Transactional
    public void goFinish(Long id) {
        DeskIng deskIng = new DeskIng();
        deskIng.setId(id);
        deskIng.setStatus(MathDeskState.FINISH.getValue());
        deskIng.setBak2(MyConstat.DESK_FINISH);
        deskIng.setBak3(MyConstat.DESK_FINISH);
        deskIng.setFinishTime(DateUtils.getNowDate());
        updateDeskIng(deskIng);
        deskIng = deskIngMapper.selectDeskIngById(id);
        TTablenumber tTablenumber = new TTablenumber();
        tTablenumber.setId(deskIng.getDeskId());
        tTablenumber.setStatus(TablenumberState.NO_LOCK.getValue());
        tTablenumberService.updateTTablenumber(tTablenumber);
        DeskMember member = new DeskMember();
        member.setDeskId(id);
        List<DeskMember> deskMemberList = deskMemberService.selectDeskMemberList(member);
        for (DeskMember deskMember : deskMemberList) {
            //会员本桌结束
            DeskMember member1 = new DeskMember();
            member1.setId(deskMember.getId());
            member1.setBak3(MyConstat.DESK_FINISH);
            deskMemberService.updateDeskMember(member1);
            TablePool tablePool = new TablePool();
            tablePool.setMemberId(deskMember.getMemberId());
            List<TablePool> tablePoolList = tablePoolService.selectTablePoolList(tablePool);
            if (!deskMember.getState().equals(MyConstat.START)) {
                tablePool.setMemberName(deskMember.getMemberName());
                tablePool.setDeskNumber(deskMember.getDeskId());
                if (ObjectUtil.isEmpty(tablePoolList) && tablePoolList.size() == 0) {

                    tablePool.setState(deskMember.getState());
                    tablePoolService.updateTablePoolmy(tablePool.getMemberId());
                    try {

                        tablePoolService.insertTablePool(tablePool);

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    tablePool.setId(tablePoolList.get(0).getId());
                    tablePool.setState(deskMember.getState());
                    tablePool.setDeskNumber(null);
                    tablePool.setIsStart("0");
                    try {

                        tablePoolService.updateTablePoolByMemberId(tablePool);

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            } else {

                tablePoolService.deleteTablePoolById(ObjectUtil.isNotNull(tablePoolList) && tablePoolList.size() != 0 ? tablePoolList.get(0).getId() : 1L);
            }
        }

    }

    @Override
    public List<DeskIng> selectFinshList(DeskIng deskIng) {
        return deskIngMapper.selectDeskIngList(deskIng);

    }

    @Override
    public List<DeskIngVo> coverList(List<DeskIng> deskIngList) {
        List<DeskIngVo> voList = new ArrayList<>();
        if (ObjectUtil.isNotEmpty(deskIngList)) {
            for (DeskIng ing : deskIngList) {
                DeskIngVo vo = new DeskIngVo();
                BeanUtils.copyProperties(ing, vo);
                if (ObjectUtil.isNotNull(vo.getStartTime())) {
                    vo.setDuration(DateUtils.getDatePoor(DateUtils.getNowDate(), vo.getStartTime()));
                }
                DeskMember member = new DeskMember();
                member.setDeskId(ing.getId());
                List<DeskMember> memberList = deskMemberService.selectDeskMemberList(member);
                for (int i = 0; i < memberList.size(); i++) {
                    DeskMember deskMember = memberList.get(i);
                    TMember tMember = tMemberService.selectTMemberById(deskMember.getMemberId());
                    if (i == 0) {
                        vo.setOneId(deskMember.getMemberId());

                        vo.setOneName(tMember.getName());
                    }
                    if (i == 1) {
                        vo.setTwoId(deskMember.getMemberId());
                        // TMember two= tMemberService.selectTMemberById(deskMember.getMemberId());
                        vo.setTwoName(tMember.getName());
                    }
                    if (i == 2) {
                        vo.setThreeId(deskMember.getMemberId());
                        //TMember three= tMemberService.selectTMemberById(deskMember.getMemberId());
                        vo.setThreeName(tMember.getName());
                    }
                    if (i == 3) {
                        vo.setFourId(deskMember.getMemberId());
                        // foure= tMemberService.selectTMemberById(deskMember.getMemberId());

                        vo.setFourName(tMember.getName());
                    }
                }
                voList.add(vo);
            }

        }
        return voList;
    }


}
